package com.ruanyun.sys.modular.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruanyun.base.pojo.node.ZTreeNode;
import com.ruanyun.base.pojo.page.LayuiPageFactory;
import com.ruanyun.sys.modular.system.entity.SysDistrict;
import com.ruanyun.sys.modular.system.model.params.SysDistrictParam;
import com.ruanyun.sys.modular.system.model.SysDistrictDto;
import com.ruanyun.sys.modular.system.service.SysDistrictService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.ruanyun.sys.modular.system.warpper.DeptWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 控制器
 *
 * @author wangchuan
 * @Date 2019-08-09 11:24:12
 */
@Controller
@RequestMapping("/sysDistrict")
public class SysDistrictController extends BaseController {

    private String PREFIX = "/modular/system/sysDistrict";

    @Autowired
    private SysDistrictService sysDistrictService;

    /**
     * 跳转到主页面
     *
     * @author wangchuan
     * @Date 2019-08-09
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/sysDistrict.html";
    }

    /**
     * 新增页面
     *
     * @author wangchuan
     * @Date 2019-08-09
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/sysDistrict_add.html";
    }

    /**
     * 编辑页面
     *
     * @author wangchuan
     * @Date 2019-08-09
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/sysDistrict_edit.html";
    }

    /**
     * 新增接口
     *
     * @author wangchuan
     * @Date 2019-08-09
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(SysDistrictParam sysDistrictParam) {
        this.sysDistrictService.add(sysDistrictParam);
        SysDistrict pEntity = new SysDistrict();
        pEntity.setDistrictCode(sysDistrictParam.getDistrictCode());
        QueryWrapper<SysDistrict> queryWrapper = new QueryWrapper<>(pEntity);
        SysDistrict entity = this.sysDistrictService.getOne(queryWrapper);
        return ResponseData.success(entity);
    }

    /**
     * 编辑接口
     *
     * @author wangchuan
     * @Date 2019-08-09
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(SysDistrictParam sysDistrictParam) {
        this.sysDistrictService.update(sysDistrictParam);
        return ResponseData.success(sysDistrictParam.getDistrictName());
    }

    /**
     * 删除接口
     *
     * @author wangchuan
     * @Date 2019-08-09
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(SysDistrictParam sysDistrictParam) {
        this.sysDistrictService.delete(sysDistrictParam);
        return ResponseData.success(sysDistrictParam.getDistrictId());
    }

    /**
     * 查看详情接口
     *
     * @author wangchuan
     * @Date 2019-08-09
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(SysDistrictParam sysDistrictParam) {
        SysDistrict detail = this.sysDistrictService.getById(sysDistrictParam.getDistrictId());
        SysDistrictDto sysDistrictDto=this.sysDistrictService.getSysDistrictDto(detail);
        return ResponseData.success(sysDistrictDto);
    }


    /**
     * 获取部门的tree列表，ztree格式
     *
     * @author administrator
     * @Date 2018/12/23 4:56 PM
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree(@RequestParam(value = "id", required = false) String id) {
        List<ZTreeNode> tree ;
        if(id==null||id=="") {
            tree =this.sysDistrictService.treeInit();
        }else{
            tree =this.sysDistrictService.tree(Long.valueOf(id));
        }
        return tree;
    }

    /**
     * 获取所有部门列表
     *
     * @author administrator
     * @Date 2018/12/23 4:57 PM
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(value = "condition", required = false) String condition,
                       @RequestParam(value = "code", required = false) String code,
                       @RequestParam(value = "id", required = false) String id) {
        Page<Map<String, Object>> list ;
        if(id != null&&!id.equals("")){
            String path = this.sysDistrictService.selectId(Long.valueOf(id)).getPath();
            list = this.sysDistrictService.list(condition,code, path);
        }else{
            String path = null;
            list = this.sysDistrictService.list(condition,code, path);
        }
        Page<Map<String, Object>> wrap = new DeptWrapper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }

}


